#include "WellSizedVector.h"
#include "PositionVector.h"
#include <vector>
#include <string>
#include <sstream>
#include <iostream>


using namespace std;

void WellSizedVector::initializeVector() {
	documents.reserve(vectorIndex);
	vectorIndex = 0;
}

//void WellSizedVector::put(uint id, uint myPosition) {
////	cout << "indice: " << vectorIndex << "\tcapacidade: " << documents.capacity() << endl;
//	if (documents.capacity() > vectorIndex) {
////		cout << "Inserindo " << id << " no indice " << vectorIndex << endl;
//		documents.push_back(PositionVector(id, myPosition));
//		vectorIndex++;
//	}
//	else
//		cout << "Tentativa de inserir palavra, mas nao cabe no vetor. Nao farei nada!!!" << endl;
//}

void WellSizedVector::add(uint id, list<unsigned short> * textPositions) {
//	cout << "id:  " << id << " step: " << step << endl;
//	cout << documents.capacity() << endl;
//	//terrivel - melhorar
//	for (uint i = 0; i < documents.size(); i++)
//		if (documents[i].getId() == id)
	list<unsigned short>::iterator it;
	it = textPositions->begin();
	documents.push_back( PositionVector(id, *it) );
	documents[vectorIndex].initializeVector(textPositions->size());
	vectorIndex++;
	it++;
	while ( it != textPositions->end() ) {
//		cout << "cap: " << documents.capacity() << " vI: " << vectorIndex << endl;
		documents[vectorIndex-1].insertPosition(*it);
		it++;
	}
}

string WellSizedVector::listDocs() {
//	cout << "Entrei no listDocs() " << documents.size() << endl;
	vector<PositionVector>::iterator it;
	string docs = "";
	it = documents.begin();
	stringstream aux1;
	aux1 << it->getId();
	docs += aux1.str() + it->showPositions();
	it++;
	while( it != documents.end() ) {
//		cout << "AEEEE " << *it << endl;
		stringstream aux;
		aux << it->getId();
		docs += ", " + aux.str() + it->showPositions();
		it++;
	}
	stringstream aux2;
	aux2 << documents.capacity();
	return docs;
}
